package com.om.opensourway;

import Utils.HttpClientUtils;
import clojure.lang.Obj;
import org.apache.commons.io.FileUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class ParseWeibo {
    public static void main(String[] args) {
//        String orgs = "opengauss"; //社区
//        String file = "C:\\code\\weiboSpider\\weibo_spider\\weibo\\openGauss\\7463406800.json";
//        String userIndex = "opengauss_weibo_user_20210317";
//        String weiboIndex = "opengauss_weibo_20210317";

        String orgs = args[0]; //社区
        String file = args[1];
        String userIndex = args[2];
        String weiboIndex = args[3];

        parseData(file, orgs, userIndex, weiboIndex);
    }

    private static void parseData(String file, String orgs, String userIndex, String weiboIndex) {
        try {
            RestHighLevelClient restHighLevelClient = HttpClientUtils.restClient();
            BulkRequest request = new BulkRequest();

            String content = FileUtils.readFileToString(new File(file), "UTF-8");
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(content);

            JsonNode userNode = jsonNode.get("user");
            HashMap user = objectMapper.convertValue(userNode, HashMap.class);
            user.put("orgs", orgs);
            user.put("update_at", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss+08:00")));
            String nickname = user.get("nickname").toString();
            request.add(new IndexRequest(userIndex, "_doc", user.get("id").toString()).source(user));

            ArrayList<String> publishTimes = new ArrayList<>();
            Iterator<JsonNode> weiboNodes = jsonNode.get("weibo").getElements();
            while (weiboNodes.hasNext()) {
                JsonNode weiboNode = weiboNodes.next();
                HashMap weibo = objectMapper.convertValue(weiboNode, HashMap.class);
                weibo.put("user_nickname", nickname);
                String publish_time = weiboNode.get("publish_time").getTextValue().replace(" ", "T") + ":00+08:00";
                weibo.put("publish_time", publish_time);
                request.add(new IndexRequest(weiboIndex, "_doc", weibo.get("id").toString()).source(weibo));
            }

            if (request.requests().size() != 0) restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
            restHighLevelClient.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
